package org.smi.jsonDemo;

//import com.alibaba.fastjson2.JSONObject;
//import com.alibaba.fastjson2.TypeReference;
import org.smi.answer.DoJSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.json.JSONUtil;

public class Test<T> {

    public static void main(String[] args) {
        Test test = new Test();

        DoJSON doJSON = new DoJSON();
        String jsonStr = doJSON.readFile("/Users/beyondsoft/Documents/title.json");
        List<TitleInfoVBO> titleInfoVBOList = test.json2Bean(jsonStr);
//        titleInfoVBOList.forEach(System.out::println);

        TitleInfoVBO reqDesc = null;
        TitleInfoVBO funcDesc = null;
        for (TitleInfoVBO t : titleInfoVBOList) {
            if (t.getTitleText().contains("业务需求描述")) {
                reqDesc = t;
            }
            if (t.getTitleText().contains("业务功能描述")) {
                funcDesc = t;
            }
        }

        List<TitleInfoVBO> level1List = new ArrayList<>();
        Integer startLevel = 0;
        String startTitleId = "";

        if (funcDesc != null) {
            // 1. 存在业务功能描述：funcDesc，parentId == 【业务功能描述】+ 1
            startLevel = funcDesc.getTitleLevel();
            startTitleId = funcDesc.getTitleId();
        } else if (funcDesc == null && reqDesc != null) {
            // 2. 不存在业务功能描述funcDesc，但是存在业务需求描述reqDesc ，parentId == 【业务需求描述】+ 1
            startLevel = reqDesc.getTitleLevel();
        }
        for (TitleInfoVBO t : titleInfoVBOList) {
            // 业务功能描述的子节点
            if (t.getTitleLevel() == startLevel + 1 && startTitleId.equals(t.getTitleParentId())) {
                level1List.add(t);
            }
        }
        level1List.forEach(System.out :: println);

        // 处理二级标题
        Map<String, List<TitleInfoVBO>> r2Map = new HashMap<>();
        for (TitleInfoVBO t : level1List) {
            startLevel = t.getTitleLevel();
            startTitleId = t.getTitleId();
            List<TitleInfoVBO> level2List = new ArrayList<>();
            for (TitleInfoVBO r : titleInfoVBOList) {
                // 业务功能描述的子节点
                if (r.getTitleLevel() == startLevel + 1 && startTitleId.equals(r.getTitleParentId())) {
                    level2List.add(r);
                }
            }
            r2Map.put(t.getTitleText(), level2List);
        }

        System.out.println(r2Map);

        // 处理三级标题
        Map<String, Map<String, List<TitleInfoVBO>>> r3Map = new HashMap<>();
        // 第一个for，这里循环的是一级标题集合，getValue是二级标题，getKey是一级标题
        for (Map.Entry<String, List<TitleInfoVBO>> entry : r2Map.entrySet()) {
            // 下面的任务是填充r2Map
            Map<String, List<TitleInfoVBO>> r2SubMap = new HashMap<>();
            r3Map.put(entry.getKey(), r2SubMap);

            // 此处是for二级标题
            for (TitleInfoVBO tt : entry.getValue()) {
                startLevel = tt.getTitleLevel();
                startTitleId = tt.getTitleId();
                List<TitleInfoVBO> level3List = new ArrayList<>();
                for (TitleInfoVBO r : titleInfoVBOList) {
                    // 业务功能描述的子节点
                    if (r.getTitleLevel() == startLevel + 1 && startTitleId.equals(r.getTitleParentId())) {
                        level3List.add(r);
                    }
                }
                r2SubMap.put(tt.getTitleText(), level3List);
            }
        }
        System.out.println("-----------");
        System.out.println(r3Map);
    }



    /*
    if (t.getTitleLevel() == startLevel + 2) {
        level2List.add(t);
    }
            if (t.getTitleLevel() == startLevel + 3) {
        level3List.add(t);
    }
*/

//    public List<TitleInfoVBO> json2Bean(String jsonStr) {
//        List<TitleInfoVBO> list = JSONObject.parseObject(jsonStr,
//                new TypeReference<List<TitleInfoVBO>>() {
//                });
//        return list;
//    }

    public List<TitleInfoVBO> json2Bean(String jsonStr) {
        List<TitleInfoVBO> list = JSONUtil.toList(jsonStr, TitleInfoVBO.class);
        return list;
    }
}
